<?php
/**
 +----------------------------------------------------------
 * 订单发票列表
 +----------------------------------------------------------
 * Author: ExBOY
 * Time: 2014-03-18 $ 20160603 更新 wangjianjun
 * [Ecos!] (C)2003-2014 Shopex Inc.
 +----------------------------------------------------------
 */
class invoice_finder_order{
    public $addon_cols = 'id,order_id,mode,is_status,sync,ship_area,ship_addr,shop_id,dateline,is_print,print_num';//调用字段
    
    //操作
    var $column_edit  = '操作';
    var $column_edit_order = 5;
    var $column_edit_width = '150';
    function column_edit($row){
        $id = $row['id'];
        $order_id = $row[$this->col_prefix . 'order_id'];
        $mode = intval($row[$this->col_prefix.'mode']);
        $is_status = intval($row[$this->col_prefix.'is_status']);
        $sync = intval($row[$this->col_prefix.'sync']);
        $shop_id = $row[$this->col_prefix.'shop_id'];
        $finder_id = $_GET['_finder']['finder_id'];
        
        //获取店铺发票配置信息 来决定是否可以进行电子发票操作
        $mdlInOrderSet = app::get('invoice')->model('order_setting');
        $invoice_setting = $mdlInOrderSet->dump(array("shop_id"=>$shop_id));
        $show_einvoice_operating_links = false;
        if($invoice_setting["einvoice_operating_conditions"] == 1 || !$invoice_setting["einvoice_operating_conditions"]){
            //选择了 已发货 或者是 空配置
            $delivered_arr = array("已发货","部分退货","已退货");
            if(in_array($row["column_ship_status"],$delivered_arr)){
                $show_einvoice_operating_links = true;
            }
        }
        if($invoice_setting["einvoice_operating_conditions"] == 2){
            //选择了 已付款
            $paid_arr = array("已支付","部分退款","全额退款");
            if(in_array($row["column_pay_status"],$paid_arr)){
                $show_einvoice_operating_links = true;
            }
        }
        
        //作废
        $doCancel = sprintf('<a href="javascript:if (confirm(\'是否确认作废该张发票？\')){W.page(\'index.php?app=invoice&ctl=admin_order&act=doCancel&id=%s&order_id=%s&finder_id=%s\', $extend({method: \'get\'}, JSON.decode({})), this);}void(0);" target="">作废</a>', $id, $order_id, $finder_id);
        //冲红 同上 作废的传递参数 修改了 提示语句内容及文字描述 
        $doCancelE = sprintf('<a href="javascript:if (confirm(\'是否确认冲红该张电子发票？\')){W.page(\'index.php?app=invoice&ctl=admin_order&act=doCancel&id=%s&order_id=%s&finder_id=%s\', $extend({method: \'get\'}, JSON.decode({})), this);}void(0);" target="">冲红</a>', $id, $order_id, $finder_id);
        //开票
        $doBilling = sprintf('<a href="javascript:if (confirm(\'是否确认进行开票操作？开票信息可在列表查看列展开处进行查看。\')){W.page(\'index.php?app=invoice&ctl=admin_order&act=doBilling&id=%s&order_id=%s&finder_id=%s\', $extend({method: \'get\'}, JSON.decode({})), this);}void(0);" target="">开票</a>', $id, $order_id, $finder_id);
        //编辑
        $edit = '<a href="index.php?app=invoice&ctl=admin_order&act=edit&id='.$id.'&finder_id='.$finder_id.'" target="dialog::{width:800,height:630,title:\'编辑发票信息\'}">编辑</a>';
        //新建发票信息
        $addNewSame = '<a href="index.php?app=invoice&ctl=admin_order&act=addNewSame&id='.$id.'&finder_id='.$finder_id.'" target="dialog::{width:800,height:630,title:\'新建发票信息\'}">新建发票信息</a>';
        if($mode == 1){
            //电子发票预览和上传
            if($is_status == 1 && in_array($sync, array(3,5))){
                //开蓝成功(包括开蓝成功和冲红失败)
                $invoice_blue_preview = '<a href="index.php?app=invoice&ctl=admin_order&act=preview&id='.$id.'&type=blue" target="_blank">预览</a>';
                if($show_einvoice_operating_links){
                    $invoice_blue_upload = kernel::single('invoice_common')->get_upload_link($id,$shop_id,$order_id,$finder_id,"1");
                }
            }
            if($is_status == 2 && $sync == 6){
                //冲红成功
                $invoice_red_preview = '<a href="index.php?app=invoice&ctl=admin_order&act=preview&id='.$id.'&type=red" target="_blank">预览</a>';
                if($show_einvoice_operating_links){
                    $invoice_red_upload = kernel::single('invoice_common')->get_upload_link($id,$shop_id,$order_id,$finder_id,"2");
                }
            }
        }
        
        //显示逻辑
        $link_arr = array();
        switch ($is_status){
            case 0:
                //未开票状态
                if($mode == 1){
                    //电子发票
                    if(in_array($sync, array(0,1,2))){
                        //开蓝票中状态
                        if($sync == 1){
                            //符合现实开票条件 并且过了连续点击限制时间
                            if($show_einvoice_operating_links && !cachecore::fetch($id."_blue")){
                                $link_arr[] = $doBilling;
                            }
                        }else{
                            //未开票和开蓝失败 
                            //符合开票条件
                            if($show_einvoice_operating_links){
                                $link_arr[] = $doBilling;
                            }
                            //可以编辑或者作废
                            $link_arr[] = $edit;
                            $link_arr[] = $doCancel;
                            /*口子留着 接口已经联调通 暂时不用
                            if($sync == 2){
                                //再次手动获取开票成功后的开票结果 开蓝失败2
                                $getEinvoiceCreateResult = sprintf('<a href="javascript:if (confirm(\'确认需要再次获得此蓝票信息吗？如获取开票结果成功后会同步开票状态为开蓝成功 请耐心等待\')){W.page(\'index.php?app=invoice&ctl=admin_order&act=getEinvoiceCreateResult&billing_type=1&id=%s&finder_id=%s\', $extend({method: \'get\'}, JSON.decode({})), this);}void(0);" target="">获取蓝票信息</a>', $id, $finder_id);
                            }*/
                        }
                    }
                }else{
                    $link_arr[] = $doBilling;
                    $link_arr[] = $edit;
                    $link_arr[] = $doCancel;
                }
                break;
            case 1:
                //已开票状态
                if($mode == 1){
                    //电子发票
                    if(in_array($sync, array(3,4,5))){
                        //开红票中
                        if($sync == 4){
                            //符合现实开票条件 并且过了连续点击限制时间
                            if($show_einvoice_operating_links && !cachecore::fetch($id."_red")){
                                $link_arr[] = $doCancelE;
                            }
                        }else{
                            //开蓝成功 冲红失败
                            //符合冲红条件
                            if($show_einvoice_operating_links){
                                $link_arr[] = $doCancelE;
                            }
                            /*口子留着 接口已经联调通 暂时不用
                            if($sync == 5){
                                //再次手动获取开票成功后的开票结果  冲红失败5
                                $getEinvoiceCreateResult = sprintf('<a href="javascript:if (confirm(\'确认需要再次获得此红票信息吗？如获取开票结果成功后会同步开票状态为冲红成功 请耐心等待\')){W.page(\'index.php?app=invoice&ctl=admin_order&act=getEinvoiceCreateResult&billing_type=2&id=%s&finder_id=%s\', $extend({method: \'get\'}, JSON.decode({})), this);}void(0);" target="">获取红票信息</a>', $id, $finder_id);
                            }*/
                        }
                    }
                    if($invoice_blue_preview){
                        $link_arr[] = $invoice_blue_preview;
                    }
                    if($invoice_blue_upload){
                        $link_arr[] = $invoice_blue_upload;
                    }
                }else{
                    $link_arr[] = $doCancel;
                }
                break;
            case 2:
                //已作废状态
                $result = kernel::single('invoice_check')->checkCreate($order_id);
                if($result){
                    $link_arr[] = $addNewSame;
                }
                if($invoice_red_preview){
                    $link_arr[] = $invoice_red_preview;
                }
                if($invoice_red_upload){
                    $link_arr[] = $invoice_red_upload;
                }
                break;
        }
        
        return implode(" | ",$link_arr);
        
    }
    
    var $column_ship_area = "客户收货地区";
    var $column_ship_area_width = "150";
    function column_ship_area($row){
        $ship_area = $row[$this->col_prefix.'ship_area'];
        $areaArr = explode(':',$ship_area);
        return str_replace('/',' ',$areaArr[1]);
    }
    
    var $column_ship_addr = "客户地址";
    var $column_ship_addr_width = "150";
    function column_ship_addr($row){
        $ship_addr = $row[$this->col_prefix.'ship_addr'];
        $ship_area = $row[$this->col_prefix.'ship_area'];
        $areaArr = explode(':',$ship_area);
        return str_replace('/',' ',$areaArr[1]).$ship_addr;
    }
    
    var $column_sync = "状态同步";
    var $column_sync_width = "60";
    var $column_sync_order = "8";
    function column_sync($row){
        $mode = intval($row[$this->col_prefix.'mode']);
        if($mode == 1){
            //电子发票返回同步状态
            $order_Obj = app::get('invoice')->model('order');
            $columns = $order_Obj->schema;
            $sync = $row[$this->col_prefix.'sync'];
            return $columns["columns"]["sync"]["type"][$sync];
        }else{
            //纸质发票没有此项
            return "-";
        }
    }
    
    var $column_is_print = "打印";
    var $column_is_print_width = "50";
    function column_is_print($row){
        $mode = intval($row[$this->col_prefix.'mode']);
        if($mode == 1){
            //电子发票没有此项
            return "-";
        }else{
            //纸质发票返回打印状态
            $is_print = $row[$this->col_prefix.'is_print'];
            $return_value = "否";
            if(intval($is_print) == 1){
                $return_value = "是";
            }
            return $return_value;
        }
    }
    
    var $column_print_num = "打印次数";
    var $column_print_num_width = "50";
    function column_print_num($row){
        $mode = intval($row[$this->col_prefix.'mode']);
        if($mode == 1){
            //电子发票没有此项
            return "-";
        }else{
            //纸质发票返回打印状态
            $print_num = $row[$this->col_prefix.'print_num'];
            return $print_num;
        }
    }
    
    //额外显示字段 提醒项
    var $column_remind = '提醒项';
    var $column_remind_width = '80';
    function column_remind($row){
        $is_status = intval($row[$this->col_prefix.'is_status']);
        if($is_status == "1"){
            //已开票状态
            $mdlOmeRePr = app::get('ome')->model('return_product');
            $order_id = $row[$this->col_prefix . 'order_id'];
            $billing_time = $row[$this->col_prefix . 'dateline']; //开票时间
            $rs_repr = $mdlOmeRePr->dump(array("order_id"=>$order_id,"status"=>4,"last_modified|than"=>$billing_time));
            if(!empty($rs_repr)){
                //有已完成的售后操作
                return "<div style='width:24px;padding:2px;height:16px;background-color:green;float:left;'><span style='color:#eeeeee;' title='有已完成的售后操作' alt='有已完成的售后操作'>售后</span></div>";
            }
        }
    }
    
    //发票详情
    var $detail_invoice    = '发票详情';
    function detail_invoice($id){
        $render = app::get('invoice')->render();
        
        //发票信息
        $mdlInOrder = app::get('invoice')->model('order');
        $rs_invoice_order = $mdlInOrder->dump(array("id"=>$id));
        $columnsInOrder = $mdlInOrder->schema;
        //显示开票方式
        $arr_mode = $columnsInOrder['columns']['mode']['type'];
        $rs_invoice_order["mode"] = $arr_mode[$rs_invoice_order["mode"]];
        //显示开票状态
        $arr_is_status = $columnsInOrder['columns']['is_status']['type'];
        $rs_invoice_order["is_status"] = $arr_is_status[$rs_invoice_order["is_status"]];
        //地区
        if($rs_invoice_order['ship_area']){
            $areaArr = explode(':', $rs_invoice_order['ship_area']);
            $areaArr = $areaArr[1];
            $rs_invoice_order['ship_area']  = str_replace('/', ' ', $areaArr);
        }
        //操作人
        $rs_operator = kernel::single('invoice_common')->getUserNameByUserID($rs_invoice_order["operator"]);
        $rs_invoice_order["operator"] = $rs_operator["name"];
        //发票内容
        $mdlInContent = app::get('invoice')->model('content');
        $rs_invoice_order["content"] = '商品明细';
        $rs_content = $mdlInContent->dump(array('content_id'=>$rs_invoice_order["content_id"]),'content_name');
        if($rs_content){
            $rs_invoice_order["content"] = $rs_content['content_name'];
        }
        $render->pagedata['invoice_order'] = $rs_invoice_order;
        
        //订单信息
        $mdlOmeOrders = app::get('ome')->model('orders');
        $columnsOmeOrders = $mdlOmeOrders->schema;
        $arr_pay_status = $columnsOmeOrders['columns']['pay_status']['type'];
        $arr_ship_status = $columnsOmeOrders['columns']['ship_status']['type'];
        $rs_ome_orders = $mdlOmeOrders->dump(array("order_id"=>$rs_invoice_order["order_id"]));
        //支付状态
        $rs_ome_orders["pay_status"] = $arr_pay_status[$rs_ome_orders["pay_status"]];
        //发货状态
        $rs_ome_orders['ship_status'] = $arr_pay_status[$rs_ome_orders["ship_status"]];
        //打印状态
        $rs_ome_orders['print_status'] = $rs_ome_orders['print_status'] ? '已打印' : '未打印';
        //买家留言
        $rs_ome_orders['custom_mark'] = unserialize($rs_ome_orders['custom_mark']);
        //订单备注
        $rs_ome_orders['mark_text'] = unserialize($rs_ome_orders['mark_text']);
        $render->pagedata['orders'] = $rs_ome_orders;
        
        //物流信息
        $deliveryObj = app::get('ome')->model('delivery');
        $deliveryIds = $deliveryObj->getDeliverIdByOrderId($rs_invoice_order["order_id"]);
        if($deliveryIds[0]){
            $dly = $deliveryObj->dump($deliveryIds[0]);
            $render->pagedata['dlyArr'] = $dly;
        }
        
        //查看电子发票
        $mdlInOrderElIt = app::get('invoice')->model('order_electronic_items');
        $render->pagedata['electronic_items'] = $mdlInOrderElIt->getList("*",array("id"=>$id));
        if(!empty($render->pagedata['electronic_items'])){
            foreach ($render->pagedata['electronic_items'] as $key_el_it=>&$var_el_it){
                if(intval($var_el_it["create_time"]) == 0){
                    unset($render->pagedata['electronic_items'][$key_el_it]);
                    continue;                    
                }
                $var_el_it["create_time"] = date("Y-m-d H:i:s",$var_el_it["create_time"]);
                if(intval($var_el_it["billing_type"]) == 2){
                    $var_el_it["billing_type_text"] = "红票";
                    $type = "red";
                }else{
                    $var_el_it["billing_type_text"] = "蓝票";
                    $type = "blue";
                }
                $var_el_it["preview_url"] = "index.php?app=invoice&ctl=admin_order&act=preview&id=".$var_el_it['id']."&type=$type";
            }
            unset($var_el_it);
        }
        
        return $render->fetch('admin/order_detail.html');
        
    }
    
    //发票操作日志
    var $detail_operate_logs = '发票操作日志';
    function detail_operate_logs($id){
        $render = app::get('invoice')->render();
        $logObj = app::get('ome')->model('operation_log');
        $logData = $logObj->read_log(array('obj_id'=>$id, 'obj_type'=>'order@invoice'), 0, -1);
        foreach($logData as $k=>$v){
            $logData[$k]['operate_time'] = date('Y-m-d H:i:s',$v['operate_time']);
        }
        $render->pagedata['datalist'] = $logData;
        return $render->fetch('admin/detail_log.html');
    }
    
}